﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
using Enterprise.Model.User;
using Enterprise.Service.User;
using Enterprise.Model.ZhiWu;
using Enterprise.Service.ZhiWu;
namespace Enterprise.UI.Web.Manage.WorkFlow
{
    public partial class DetailManage_Manage : System.Web.UI.Page
    {
        protected int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int NodeId = (int)Utility.sink("NodeId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            OnBindChooseObject();
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OnBindWorkFlow();
           
        }

        private void OnBindWorkFlow()
        {
            if (Id == 0)
                return;

            WorkFlowInfoService wService = new WorkFlowInfoService();
            var q = wService.WorkFlowDisp(Id);
            if (q != null)
            {
                lb_Name.Text = lb_Name1.Text = q.WFNAME;
            }
            OnCommand();
        }

        private void OnCommand()
        {
            switch (Cmd)
            {
                case "":
                    OnBindWorkFlowNodeData();
                    HiddenInput();
                    break;
                case "New":
                    HiddenDisp();
                    break;
                case "Edit":
                    OnBindWorkFlowNodeData();
                    HiddenDisp();
                    break;
                case "Delete":
                    WorkFlowNodeService nodeService = new WorkFlowNodeService();
                    var q = nodeService.WorkFlowNodeList().Where(p => p.NID == NodeId).FirstOrDefault();
                    if (q != null)
                    {
                        q.DB_Option_Action = "Delete";
                        nodeService.WorkFlowNodeExecute(q);
                    }
                    Response.Redirect("DetailManage.aspx?Id=" + Id);
                    Response.End();
                    break;
            }
        }

        private void OnBindWorkFlowNodeData()
        {
            WorkFlowNodeService nodeService = new WorkFlowNodeService();
            var q = nodeService.WorkFlowNodeList().Where(p => p.NID == NodeId).FirstOrDefault();
            if (q != null)
            {
                lb_NodeName.Text = tb_NodeName.Text = q.NNAME;
                lb_Type.Text = Utility.GetWorkFlowType(q.NTYPE);
                ListItem litype = tb_Type.Items.FindByValue(q.NTYPE.ToString());
                if (litype != null)
                    litype.Selected = true;
                lb_Object.Text = Utility.GetWorkFlowObjectType(q.NOBJECTTYPE);
                ListItem liobjecttype = tb_Object.Items.FindByValue(q.NOBJECTTYPE.ToString());
                if (liobjecttype != null)
                    liobjecttype.Selected = true;
                if (q.NOBJECTTYPE == 0)
                {
                    lb_ObjectUser.Text = UserService.GetUserName(q.NOBJECT);
                    OnBindChooseObject();
                    ListItem liuser = tb_ObjectUser.Items.FindByValue(q.NOBJECT.ToString());
                    if (liuser != null)
                        liuser.Selected = true;                    
                }
                else if (q.NOBJECTTYPE == 1)
                {
                    lb_ObjectUser.Text = ZhiwuService.GetZhiWuName(q.NOBJECT);
                    OnBindZwObject();
                    ListItem lizw = tb_ObjectUser.Items.FindByValue(q.NOBJECT.ToString());
                    if (lizw != null)
                        lizw.Selected = true;
                }
                lb_Remark.Text = tb_Remark.Text = q.NREMARK;
            }
        }

        private void HiddenInput()
        {
            tb_NodeName.Visible = false;
            tb_Type.Visible = false;
            tb_Object.Visible = false;
            tb_ObjectUser.Visible = false;
            tb_Remark.Visible = false;
            DivSubmit.Visible = false;
        }

        private void HiddenDisp()
        {
            lb_NodeName.Visible = false;
            lb_Type.Visible = false;
            lb_Object.Visible = false;
            lb_ObjectUser.Visible = false;
            lb_Remark.Visible = false;
        }

        private void OnBindChooseObject()
        {
            tb_ObjectUser.Items.Clear();
            UserService uService = new UserService();
            List<UserEntity> list = uService.UserList();
            tb_ObjectUser.DataSource = list;
            tb_ObjectUser.DataTextField = "UNAME";
            tb_ObjectUser.DataValueField = "USERID";
            tb_ObjectUser.DataBind();
            tb_ObjectUser.Items.Insert(0, new ListItem("请选择人员", "0"));
        }
        private void OnBindZwObject()
        {
            tb_ObjectUser.Items.Clear();
            ZhiwuService zwService = new ZhiwuService();
            List<ZhiwuEntity> list = zwService.ZhiWuList();
            tb_ObjectUser.DataSource = list;
            tb_ObjectUser.DataTextField = "ZWNAME";
            tb_ObjectUser.DataValueField = "ZWID";
            tb_ObjectUser.DataBind();
            tb_ObjectUser.Items.Insert(0, new ListItem("请选择职务", "0"));
        }


        protected void tb_Object_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            int obj = (int)Utility.sink(tb_Object.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            if (obj != 0 && (obj == 2 || obj == 3 || obj == 4))
            {
                ChooseObject.Visible = false;
            }
            else
            {
                ChooseObject.Visible = true;
                if (obj == 0)
                {
                    OnBindChooseObject();
                }
                else if (obj == 1)
                {
                    OnBindZwObject();
                }
            }
        }

        protected void Button1_OnClick(object sender, EventArgs e)
        {
            WorkFlowNodeService nService = new WorkFlowNodeService();
            WorkFlowNodeEntity entity = new WorkFlowNodeEntity();
            entity.WFID = Id;
            entity.NNAME = (string)Utility.sink(tb_NodeName.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            entity.NTYPE = (int)Utility.sink(tb_Type.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            entity.NOBJECTTYPE = (int)Utility.sink(tb_Object.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            entity.NOBJECT = (int)Utility.sink(tb_ObjectUser.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            entity.NREMARK = (string)Utility.sink(tb_Remark.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            switch (Cmd)
            {
                case "New":
                    entity.NORDERBY = nService.WorkFlowNodeMaxOrderBy(Id) + 1;
                    entity.DB_Option_Action = "Insert";
                    break;
                case "Update":
                    entity.DB_Option_Action = "Update";
                    entity.NID = NodeId;
                    break;
            }            
            nService.WorkFlowNodeExecute(entity);
            Response.Redirect("DetailManage.aspx?Id=" + Id);
            Response.End();
        }

    }
}